package com.game;

import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
public class LoginJFrame extends JFrame implements MouseListener {
    static ArrayList<User> list = new ArrayList<>();
    static{
        list.add(new User("123456", "123456"));
    }

    public static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789";
    public  String pathLogin = "image/login/登录按钮.png";
    public  String pathRegister = "image/login/注册按钮.png";
    public static final int CODELEN = 5 ;

    //验证码字符串
    String codeStr = "";

    //文本框
    JTextField username = new JTextField();
    JTextField password = new JTextField();
    JTextField code = new JTextField();

    JButton login = new JButton();
    JButton register = new JButton();

    public LoginJFrame(){
        //初始化界面
        initJFrame();

        //初始化显示验证码
        initCode();

        //展示界面
        initImage();

        //一直显示
        this.setVisible(true);
    }

    //初始化按钮
    private void initButton() {
        //登录按钮
        login.setBounds(123, 310, 128, 47);
        login.setIcon(new ImageIcon(pathLogin));
        login.setBorderPainted(false);  //去除按钮默认边框
        login.setContentAreaFilled(false);  //去除按钮默认背景
        this.add(login);


        //注册按钮
        register.setBounds(256, 310, 128, 47);
        register.setIcon(new ImageIcon(pathRegister));
        register.setBorderPainted(false);   //去除按钮默认边框
        register.setContentAreaFilled(false);   //去除按钮默认背景
        this.add(register);

        //给按钮添加鼠标监听
        login.addMouseListener(this);
        register.addMouseListener(this);
    }

    //初始化界面
    private void initJFrame() {
        this.setSize(488,430);

        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //设置标题
        this.setTitle("拼图单机版 v1.0登陆界面");

        //设置界面置顶
        this.setAlwaysOnTop(true);

        //设置页面居中
        this.setLocationRelativeTo(null);
    }

    //初始化界面图片
    private void initImage() {
        //清除所有图片
        this.getContentPane().removeAll();

        //添加用户名文字
        JLabel usernameText = new JLabel(new ImageIcon("image/login/用户名.png"));
        usernameText.setBounds(116, 135, 47, 17);
        this.add(usernameText);

        //添加密码文字
        JLabel passworldText = new JLabel(new ImageIcon("image/login/密码.png"));
        passworldText.setBounds(130, 195, 32, 16);
        this.add(passworldText);

        //添加验证码文字
        JLabel codeText = new JLabel(new ImageIcon("image/login/验证码.png"));
        codeText.setBounds(133, 256, 50, 30);
        this.add(codeText);

        //添加输入框
        initInputbox();

        //添加验证码
        JLabel rightCode = new JLabel();
        rightCode.setText(codeStr);
        rightCode.setBounds(300, 256, 50, 30);
        this.add(rightCode);

        //加入按钮
        initButton();

        //添加背景图片
        JLabel background = new JLabel(new ImageIcon("image/login/background.png"));
        background.setBounds(0, 0, 470, 390);
        this.getContentPane().add(background);

        //重新生成
        this.getContentPane().repaint();
    }

    //初始化输入框
    private void initInputbox() {
        //用户名输入框
        username.setBounds(195, 130, 200, 30);
        this.add(username);

        //密码输入框
        password.setBounds(195, 190, 200, 30);
        this.add(password);

        //验证码输入框
        code.setBounds(195, 256, 100, 30);
        this.add(code);


    }

    //初始化验证码
    private void initCode() {
        Random ran = new Random();
        for (int i = 0; i < CODELEN; i++) {
            codeStr += CHARACTERS.charAt(ran.nextInt(CHARACTERS.length()));
        }
    }

    //显示错误弹窗
    private void showJDialog(String content) {
        //创建弹框对象
        JDialog jDialog = new JDialog();

        //设置宽高
        jDialog.setSize(200, 150);

        //弹框置顶
        jDialog.setAlwaysOnTop(true);

        //弹框居中
        jDialog.setLocationRelativeTo(null);

        //弹窗不关闭无法完成下面的操作
        jDialog.setModal(true);

        //创建容器管理弹框文字
        JLabel warning = new JLabel(content);
        warning.setBounds(0, 0, 200, 150);
        jDialog.add(warning);

        //弹框显示出来
        jDialog.setVisible(true);
    }

    //登录功能
    private void playLogin() {
        String codeText = code.getText();
        String usernameText = username.getText();
        String passwordText = password.getText();
//        if (codeText.isEmpty()){
//            showJDialog("验证码为空！");
//        }else if (!codeText.equals(codeStr)){
//            showJDialog("验证码输入错误！");
//        }else {
//            if (usernameText.isEmpty()||passwordText.isEmpty()){
//                showJDialog("用户名或者密码为空！");
//            }else if (!checkUser(usernameText, passwordText)) {
//                showJDialog("用户名或者密码输入错误！");
//            }else {
//                this.setVisible(false);
//
//                new GameJFrame();
//            }
//        }
        if (codeText.equals(codeStr) && checkUser(usernameText, passwordText)){
            this.setVisible(false);
            new GameJFrame();
        }else showJDialog("输入有误！");
    }

    //检查用户名和密码
    private boolean checkUser(String username, String password){
        for (User user : list) {
            if (user.getUsername().equals(username) && user.getPassword().equals(password)){
                return true;
            }
        }
        return false;
    }

    //注册功能
    private void playRegister() {}

    @Override
    public void mouseClicked(MouseEvent e) {    //点击，跟松开

    }



    @Override
    public void mousePressed(MouseEvent e) {
        //按下
        if (e.getSource() == login){
            pathLogin = "image/login/登录按下.png";
            initImage();
        } else if (e.getSource() == register) {
            pathRegister = "image/login/注册按下.png";
            initImage();
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        //松开
        if (e.getSource() == login){
            pathLogin = "image/login/登录按钮.png";
            playLogin();
            codeStr = "";
            initCode();//随机验证码
            initImage();
        } else if (e.getSource() == register) {
            pathRegister = "image/login/注册按钮.png";
            initImage();
            playRegister();
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

}
